package tx;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import util.DBUtils;

public class BatchTest {
	
	@Test
	public void batch() throws SQLException {
		Connection c = DBUtils.getConnection();
		c.setAutoCommit(false);
		
		String sql = "insert into card values(?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		
		for(int i = 3 ;i <= 10000; i++) {
			// 循环的往批处理缓存中添加
			ps.setInt(1,i);
			ps.setInt(2, 100+i);
			ps.addBatch();
			// 每500条件，批量执行一次，执行后清空一次
			if (i % 500 == 0) {
				int[] rows = ps.executeBatch();
				ps.clearBatch();
			}
		}
		// 提交事务
		c.commit();
		c.setAutoCommit(true);
	}
}
